<template>
  <!-- 工具条 -->

  <div class="app-container">
    <!--查询表单-->
    <div class="search-div">
      <el-form :inline="true"
               label-width="80px"
               content-width="200px">
        <el-row>
          <el-form-item label="角色名称">
            <el-input v-model.trim="searchObj.roleName" placeholder="角色名称"></el-input>
          </el-form-item>
          <div class="spacer"></div>
          <el-form-item label="角色编码">
            <el-input v-model.trim="searchObj.roleCode" placeholder="角色编码"></el-input>
          </el-form-item>
          <div class="spacer"></div>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" :loading="loading" @click="fetchData()">搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <!-- 添加按钮-->
    <div class="tools-div">
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="add">添 加</el-button>
      <el-button type="danger"   icon="el-icon-delete" size="mini" @click="batchRemove()" >批量删除</el-button>
    </div>
    <el-table v-loading="listLoading"
              :data="list"
              stripe
              border
              style="width: 100%;margin-top: 10px;"
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"/>
      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (pageNo - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="roleName" label="角色名称"/>
      <el-table-column prop="roleCode" label="角色编码"/>
      <el-table-column prop="description" label="描述"/>
      <el-table-column prop="createTime" label="创建时间" width="160"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)">删除</el-button>
          <el-button type="warning" icon="el-icon-user" size="mini" @click="showAssignAuth(scope.row)">分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹出框-->
    <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%" >
      <el-form ref="dataForm" :model="sysRole" label-width="150px" size="small" style="padding-right: 40px;">
        <el-form-item label="角色名称">
          <el-input v-model="sysRole.roleName"/>
        </el-form-item>
        <el-form-item label="角色编码">
          <el-input v-model="sysRole.roleCode"/>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="sysRole.description"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small" icon="el-icon-refresh-right">取 消</el-button>
        <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="pageNo"
      :total="total"
      :page-size="pageSize"
      style="padding: 30px 0; text-align: center;"
      layout="prev, pager, next"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import api from '@/api/system/sysRole'
export default {
  name: "index",
  //  数据
  data() {
    return {
      list: [], //列表
      total: 0, // 总记录数
      pageNo: 1,  //页码
      pageSize: 10,  //每页大小
      searchObj: {}, // 查询条件
      multipleSelection: [], //批量删除选中的记录列表
      listLoading: false,
      loading: false,
      dialogVisible: false, // 对话框是否可见
      sysRole: {},  // 角色数据
      saveBtnDisabled: false
    }
  },
  // 页面渲染成功后获取数据
  created() {
    this.fetchData()
  },
  methods: {
    fetchData(page = 1) {
      this.listLoading = true
      this.pageNo = page
      api.getPageList(this.pageNo, this.pageSize, this.searchObj).then(
        res => {
          this.list = res.data.records
          this.total = res.data.total
          this.listLoading = false
        }
      )
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
    },
    removeDataById(id) {
      this.listLoading = true
      this.$confirm("是否删除该数据", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.removeById(id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功');
            this.fetchData()
          }
        })
      })
      this.listLoading = false
    },
    add(){
      this.sysRole = {}
      this.dialogVisible = true
    },
    save(){
      api.save(this.sysRole).then(res =>{
        if(res.code === 200){
          this.$message.success('添加成功')
          this.dialogVisible = false
          this.fetchData()
        }
      })
    },
    edit(id) {
      this.dialogVisible = true
      this.fetchDataById(id)
    },
    update(){
      api.updateById(this.sysRole).then(res =>{
        if(res.code === 200){
           this.$message.success('更新成功')
          this.dialogVisible = false
        }
        this.fetchData()
      })

    },
    fetchDataById(id){
      api.getById(id).then(res =>{
        this.sysRole = res.data
      })
    },
    saveOrUpdate(){
      this.saveBtnDisabled = true
      if(!this.sysRole.id){
        this.save()
      }else{
        this.update()
      }
    },
    batchRemove(){
      if(this.multipleSelection.length === 0){
        this.$message.warning('请选择要删除的记录');
        return
      }
      this.$confirm('是否删除数据','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() =>{
        let ids = this.multipleSelection.map(s => s.id)
        api.removeIdBatch(ids).then(res =>{
          if(res.code === 200){
            this.$message.success('删除成功')
          }
          this.fetchData()
        })
      })

    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    showAssignAuth(row){
      this.$router.push(`/system/assignAuth?id=${row.id}&roleName=${row.roleName}`)
    }
  }
}
</script>

<style scoped>
.spacer {
  display: inline-block;
  width: 50px;
}

.el-button--primary {
  box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.1);
}

.el-button--primary {
  background-image: linear-gradient(to right, #00C9FF , #92FE9D);
  color: #fff;
}

</style>
